const http = require('http')
const path = require('path')
const fs = require('fs')
const url = require('url')

http.createServer((req,res)=>{
    const pathname = url.parse(req.url).pathname
    if (pathname==='/'){
        const html = fs.readFileSync(path.resolve(__dirname, 'index.html'))
        res.writeHeader(200,{
            'Content-Type': 'text/html',
            // 'Content-Security-Policy': 'default-src http: https:' // 限制不能使用行内js，防止他人注入脚本
            // 'Content-Security-Policy': "script-src 'self'; form-action 'self'; report-uri /report" // 限制访问外链js、img，form提交跳转外链
        })
        res.write(html)
        res.end()
    } else {
        res.writeHeader(200,{
            'Content-Type': 'application/javascript'
        })
        res.end('console.log("loaded srcipt")')
    }
}).listen(8888)